import request from '@/helpers/request'
import {friendlyDate} from '@/helpers/utils'
const URL = {
    GET: '/notebooks',
    UPDATE: '/notebooks/:id',
    DELETE: '/notebooks/:id',
    ADD: '/notebooks'
}
export default {
    getNotebookList() {
        return new Promise((resolve, reject) => {
            request(URL.GET, 'GET').then((res) => {
                //按时间排序
                const arr = res.data.sort((notebook1, notebook2) => notebook2.createdAt.localeCompare(notebook1.createdAt))
                arr.forEach(notebook => {
                    notebook.friendlyCreatedAt = friendlyDate(notebook.createdAt)
                    notebook.friendlyUpdatedAt=friendlyDate(notebook.updatedAt)
                })
                res.data = arr
                resolve(res)
            }).catch((err) => {
                reject(err)
            })
        })
        return 
    },
    updateNotebook(notebookId,{title=''}={title:''}) {
        return request(URL.UPDATE.replace(':id',notebookId),'PATCH',{title})
    },
    addNotebook({ title = '' } = { title: '' }) {
        return new Promise((resolve, reject) => {
            request(URL.ADD, 'POST', { title }).then((res) => {
                res.data.friendlyCreatedAt = friendlyDate(res.data.createdAt)
                res.data.friendlyUpdatedAt = friendlyDate(res.data.updatedAt)
                resolve(res)
            }).catch((res)=>{
                reject(res)
            })
        })
      
    },
    deleteNotebook(notebookId) {
      
        return request(URL.DELETE.replace(':id',notebookId),'DELETE')
    }
}